/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JProductCustomerPriceEditor.java
 *
 * Created on 2010-10-22, 9:44:41
 */

package cn.ekuma.epos.productsfinishing;

import cn.ekuma.data.dao.bean.I_Category;
import cn.ekuma.data.ui.swing.AbstractChildDAOJEditor;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import com.openbravo.bean.CustomerCategory;
import com.openbravo.bean.ProductCustomerCategoryPrice;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValModel;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.panels.JCategoryTreeDialog;
import java.util.List;
import java.util.Properties;
import javax.swing.JLabel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

/**
 *
 * @author Administrator
 */
public class ProductCustomerPriceEditor extends AbstractChildDAOJEditor<ProductCustomerCategoryPrice,String> {

    private ComboBoxValModel m_CategoryModel;
    private List catList;
    I_DataLogicSales dlSales;

    public ProductCustomerPriceEditor(I_DataLogicSales dlSales,DirtyManager dirty) {
        super(dirty);
        initComponents();
        this.dlSales=dlSales;
        m_jCategory.addActionListener(dirty);
        m_jPriceSell.getDocument().addDocumentListener(dirty);
        m_JWholesalePrice.getDocument().addDocumentListener(dirty);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel5 = new javax.swing.JLabel();
        m_jCategory = new javax.swing.JComboBox();
        jSearchCategory = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        m_jPriceSell = new javax.swing.JTextField();

        jLabel5.setText(AppLocal.getIntString("label.prodcategory")); // NOI18N

        jSearchCategory.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/search.png"))); // NOI18N
        jSearchCategory.setFocusable(false);
        jSearchCategory.setPreferredSize(new java.awt.Dimension(35, 25));
        jSearchCategory.setRequestFocusEnabled(false);
        jSearchCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSearchCategoryActionPerformed(evt);
            }
        });

        jLabel4.setText(AppLocal.getIntString("label.prodpricesell")); // NOI18N

        m_jPriceSell.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        
        JLabel label = new JLabel(AppLocal.getIntString("label.WholesalePrice"));
        
        m_JWholesalePrice = new JTextField();
        m_JWholesalePrice.setHorizontalAlignment(SwingConstants.RIGHT);
        m_JWholesalePrice.setColumns(10);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        layout.setHorizontalGroup(
        	layout.createParallelGroup(Alignment.LEADING)
        		.addGroup(layout.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(layout.createParallelGroup(Alignment.TRAILING, false)
        				.addComponent(label, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        				.addComponent(jLabel5, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
        				.addComponent(jLabel4, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(layout.createParallelGroup(Alignment.LEADING, false)
        				.addComponent(m_JWholesalePrice)
        				.addComponent(m_jPriceSell)
        				.addComponent(m_jCategory, 0, 140, Short.MAX_VALUE))
        			.addComponent(jSearchCategory, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
        			.addContainerGap(116, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
        	layout.createParallelGroup(Alignment.LEADING)
        		.addGroup(layout.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(layout.createParallelGroup(Alignment.LEADING)
        				.addComponent(jLabel5)
        				.addComponent(m_jCategory, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
        				.addComponent(jSearchCategory, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(layout.createParallelGroup(Alignment.LEADING)
        				.addComponent(jLabel4)
        				.addComponent(m_jPriceSell, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.UNRELATED)
        			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(label)
        				.addComponent(m_JWholesalePrice, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addContainerGap(218, Short.MAX_VALUE))
        );
        this.setLayout(layout);
    }// </editor-fold>//GEN-END:initComponents

    private void jSearchCategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSearchCategoryActionPerformed
        JCategoryTreeDialog dailog=JCategoryTreeDialog.newJDialog(this);
        dailog.showDialog(catList,(I_Category) m_CategoryModel.getSelectedItem());
        if(dailog.isOK()){
            dirty.setDirty(true);
            m_CategoryModel.setSelectedItem(dailog.getSelected());
        }
}//GEN-LAST:event_jSearchCategoryActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JButton jSearchCategory;
    private javax.swing.JComboBox m_jCategory;
    private javax.swing.JTextField m_jPriceSell;
    private JTextField m_JWholesalePrice;
    // End of variables declaration//GEN-END:variables

    @Override
    public String getTabName() {
        return  AppLocal.getIntString("label.CustomerCategoryPrice");
    }

    @Override
    public void activate() throws BasicException {
        // Load the taxes logic
        catList=dlSales.list(CustomerCategory.class);
        m_CategoryModel = new ComboBoxValModel(catList);
        m_jCategory.setModel(m_CategoryModel);
    }


    @Override
    public ProductCustomerCategoryPrice createNew(Properties prop) {
        return new ProductCustomerCategoryPrice(parentNode.getParentKey().getKey());
    }

    @Override
    public void rebuild(ProductCustomerCategoryPrice obj) throws BasicException {
         obj.setCustomerCategory((CustomerCategory) m_CategoryModel.getSelectedItem());
         obj.setM_dCustomerCategoryPrice((Double)Formats.CURRENCY.parseValue(m_jPriceSell.getText()));
         obj.setM_dWholesalePrice((Double)Formats.CURRENCY.parseValue(m_JWholesalePrice.getText()));
    }

    @Override
    public void updateUI(boolean editAble, int state) {
        m_jCategory.setEditable(editAble);
        m_jPriceSell.setEditable(editAble);
        m_JWholesalePrice.setEditable(editAble);
        CustomerCategory customerCategory=obj.getCustomerCategory();
        m_CategoryModel.setSelectedKey(customerCategory==null?null:customerCategory.getID());
        m_jPriceSell.setText(Formats.CURRENCY.formatValue(obj.getM_dCustomerCategoryPrice()));
        m_JWholesalePrice.setText(Formats.CURRENCY.formatValue(obj.getM_dWholesalePrice()));
    }

    @Override
    public Class getEditClass() {
       return  ProductCustomerCategoryPrice.class;
    }

    public void refresh() {
       
    }

    @Override
    public String validator(ProductCustomerCategoryPrice obj) {
        if(obj.getCustomerCategory()==null)
           return AppLocal.getIntString("message.CustomerCategoryNoNullAble");
        return super.validator(obj);
    }
}
